/**
 * Created by Frings on 2017/5/17.
 */
/**
 *
 * @param property_type_ele -- 属性类型select
 * @param property_type_default_value -- 属性类型select 默认值
 * @param property_type_control_ele   -- 属性值设置控件 select
 * @constructor
 */


//属性类型
function PropertyType(element, property_type, property_control, product_property_control) {
    this.Element = element;
    this.PropertyType = property_type;
    this.PropertyControl = property_control;
    this.ProductPropertyControl = product_property_control;
}

PropertyType.prototype = {
    render: function () {
        //初始化属性类型包含的值
        this.Element.append('<option value="">请选择</option>');
        this.Element.append('<option value="string">文本</option>');
        this.Element.append('<option value="number">数字</option>');
        this.Element.append('<option value="enum">枚举</option>');
        this.Element.append('<option value="list">列表</option>');
        this.Element.append('<option value="table">表格</option>');
        this.Element.append('<option value="file">文件</option>');
        this.Element.append('<option value="auto">自动</option>');

        var that = this;
        //设置change事件
        // this.Element.change(function () {
        //     that.change();
        // });

        //编辑是有值进来 初始化 属性
        var options = this.Element.find("option");
        if (this.PropertyType) {
            for (var i = 0; i < options.length; i++) {
                var op = $(options[i]);
                if (this.PropertyType == op.val()) {
                    op.attr('selected', 'selected');
                    break;
                }
            }

            this.PropertyControl.render(this.PropertyType);

        }


    },
    change: function () {
        //重新初始化属性控件列表
        var option_value = this.Element.children('option:selected').val();

        this.PropertyControl.SettingControl = null;
        this.PropertyControl.render(option_value);

        //初始化属性规则
        this.ProductPropertyControl = new ProductProperty();
        if (option_value == "auto" || option_value == "file") {
            this.ProductPropertyControl.PropertyType = option_value;
        }
        return this.ProductPropertyControl;
    },
    getValue: function () {
        var option_value = this.Element.children('option:selected').val();
        return option_value;
    }

};


//属性值设置控件
function PropertyControl(element, setting_control) {
    this.Element = element;
    this.SettingControl = setting_control;
}

PropertyControl.prototype = {
    render: function (property_type) {
        this.Element.empty();

        switch (property_type) {
            case "string":
                this.Element.append('<option value="text">单行文本框</option>');
                break;
            case "number":
                this.Element.append('<option value="num-text">数字框</option>');
                break;
            case "enum":
            case "list":
            case "table":
                this.Element.append('<option value="radiobox">单选框</option>');
                break;
            case "file":
                this.Element.append('<option value="file-upload">文件上传</option>');
                break;
            case "auto":
                break;
        }

        if (this.SettingControl) {
            for (var i = 0; i < this.SettingControl.length; i++) {
                var setting_control = this.SettingControl[i];

                var options = this.Element.children('option');
                for (var j = 0; j < options.length; j++) {
                    var op = $(options[j]);
                    if (setting_control == op.val()) {
                        op.attr('selected', 'selected');
                    }
                }

            }
        }
    },
    getValue: function () {
        var option_values = this.Element.children('option');
        var datas = [];
        if (option_values) {
            for (var i = 0; i < option_values.length; i++) {
                var option_value = option_values[i];
                datas.push($(option_value).val());
            }
        }

        return datas;
    }
};


//设置属性值
function ProductProperty(ele, property_type, value_rule) {
    this.Ele = ele;
    this.PropertyType = property_type;
    this.ValueRule = value_rule;

    this.Property = null;
    this.PropertyId = null;
}

ProductProperty.prototype = {
    render: function () {
        switch (this.PropertyType) {
            case "string":
                this.Property = new PropertyString(this.Ele, this.ValueRule);
                break;
            case "number":
                this.Property = new PropertyNumber(this.Ele, this.ValueRule);
                break;
            case "enum":
                this.Property = new PropertyEnum(this.Ele, this.ValueRule);
                break;
            case "list":
                this.Property = new PropertyList(this.Ele, this.ValueRule, this.PropertyId);
                break;
            case "table":
                this.Property = new PropertyTable(this.Ele, this.ValueRule);
                break;
            case "file":
                this.Property = new PropertyFile(this.Ele, this.ValueRule);
                break;
            case "auto":
                this.Property = new PropertyAuto(this.Ele, this.ValueRule);
                break;
        }
        this.Property.render();
    },
    saveValue: function () {
        if (this.Property) {
            this.ValueRule = this.Property.saveValue();
            return true;
        }
    },
    getValue: function () {
        if (this.ValueRule) {
            return this.ValueRule;
        } else {
            if (this.PropertyType == "auto" || this.PropertyType == "file") {
                this.ValueRule = {};
                this.ValueRule.type = this.PropertyType;
                return this.ValueRule;
            }
        }
    }
};


function PropertyString(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
}

PropertyString.prototype = {
    render: function () {
        this.Element.empty();
        var html = '';
        html += '<div class="form-horizontal" id="property_string_form">';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="min-length">最小长度：</label>';
        html += '<div class="col-lg-7"><input type="number" id="min-length" name="min-length" class="form-control" value=""></div>';
        html += '<label class="input-checkbox col-lg-3 label-inline"><input type="checkbox" class="label--input-inline" id="no-set-min"><span>不设置最小值</span></label>';
        html += '</div>';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="max-length">最大长度：</label>';
        html += '<div class="col-lg-7"><input type="number" id="max-length" name="max-length" class="form-control" value=""></div>';
        html += '<label class="input-checkbox col-lg-3 label-inline"><input type="checkbox" class="label--input-inline" id="no-set-max"><span>不设置最大值</span></label>';
        html += '</div>';

        html += '</div>';

        var obj = $(html);
        this.Element.append(obj);

        var that = this;
        //初始化数据
        if (this.ValueRule) {
            if (this.ValueRule.min_length.toString() != "") {
                obj.find("#min-length").val(this.ValueRule.min_length);
            } else {
                obj.find("#min-length").val("");
                obj.find("#min-length").attr("disabled", true);
                obj.find("#no-set-min").prop("checked", true);
            }

            if (this.ValueRule.max_length.toString() != "") {
                obj.find("#max-length").val(this.ValueRule.max_length);
            } else {
                obj.find("#max-length").val("");
                obj.find("#max-length").attr("disabled", true);
                obj.find("#no-set-max").prop("checked", true);
            }
        }

        //绑定事件
        obj.find("#no-set-min").click(function () {
            if ($(this).is(':checked')) {
                obj.find("#min-length").val("");
                obj.find("#min-length").attr("disabled", true);
            } else {
                obj.find("#min-length").attr("disabled", false);
            }
        });

        obj.find("#no-set-max").click(function () {
            if ($(this).is(':checked')) {
                obj.find("#max-length").val("");
                obj.find("#max-length").attr("disabled", true);
            } else {
                obj.find("#max-length").attr("disabled", false);
            }
        });
    },
    saveValue: function () {
        if (!this.ValueRule) {
            this.ValueRule = {};
        }
        var obj = this.Element;
        var min = obj.find("#min-length").val();
        var max = obj.find("#max-length").val();
        if (min != "") {
            min = parseInt(min);
        }
        if (max != "") {
            max = parseInt(max);
        }

        if (min != "" && max != "") {
            if (min >= max) {
                warningDialog("最小值不能大于等于最大值");
                return false;
            }
        }

        this.ValueRule.type = "string";
        this.ValueRule.min_length = min;
        this.ValueRule.max_length = max;

        return this.ValueRule;
    },
    getValue: function () {
        return this.ValueRule;
    }
};


function PropertyNumber(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
}

PropertyNumber.prototype = {
    render: function () {
        this.Element.empty();
        var html = "";
        html += '<div class="form-horizontal" id="property_number_form">';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="min">最小值：</label>';
        html += '<div class="col-lg-7"><input type="number" id="min" name="min" class="form-control" value=""></div>';
        html += '<label class="input-checkbox col-lg-3 label-inline"><input type="checkbox" class="label--input-inline" id="no-set-min"><span>不设置最小值</span></label>';
        html += '</div>';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="max">最大值：</label>';
        html += '<div class="col-lg-7"><input type="number" id="max" name="max" class="form-control" value=""></div>';
        html += '<label class="input-checkbox col-lg-3 label-inline"><input type="checkbox" class="label--input-inline" id="no-set-max"><span>不设置最大值</span></label>';
        html += '</div>';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="step">变化步长：</label>';
        html += '<div class="col-lg-7"><input type="number" id="step" name="step" class="form-control" value=""></div>';
        html += '<label class="input-checkbox col-lg-3 label-inline"><input type="checkbox" class="label--input-inline" id="no-set-step"><span>不设置变化步长</span></label>';
        html += '</div>';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="step">是否整型：</label>';
        html += '<label class="input-checkbox col-lg-3 label-inline"><input type="checkbox" class="label--input-inline" id="is-integer"><span>是否整型</span></label>';
        html += '</div>';

        html += '</div>';

        var obj = $(html);
        this.Element.append(obj);

        var that = this;
        //初始化数据
        if (this.ValueRule) {
            if (this.ValueRule.min.toString() != "") {
                obj.find("#min").val(this.ValueRule.min);
            } else {
                obj.find("#min").val("");
                obj.find("#min").attr("disabled", true);
                obj.find("#no-set-min").prop("checked", true);
            }

            if (this.ValueRule.max.toString() != "") {
                obj.find("#max").val(this.ValueRule.max);
            } else {
                obj.find("#max").val("");
                obj.find("#max").attr("disabled", true);
                obj.find("#no-set-max").prop("checked", true);
            }

            if (this.ValueRule.step.toString() != "") {
                obj.find("#step").val(this.ValueRule.step);
            } else {
                obj.find("#step").val("");
                obj.find("#step").attr("disabled", true);
                obj.find("#no-set-step").prop("checked", true);
            }

            if (this.ValueRule.is_integer) {
                obj.find("#is-integer").prop("checked", true);
            } else {
                obj.find("#is-integer").prop("checked", false);
            }
        }

        //初始化checkbox事件
        obj.find("#no-set-min").click(function () {
            if ($(this).is(':checked')) {
                obj.find("#min").val("");
                obj.find("#min").attr("disabled", true);
                that.ValueRule.min = "";
            } else {
                obj.find("#min").attr("disabled", false);
            }
        });

        obj.find("#no-set-max").click(function () {
            if ($(this).is(':checked')) {
                obj.find("#max").val("");
                obj.find("#max").attr("disabled", true);
                that.ValueRule.max = "";
            } else {
                obj.find("#max").attr("disabled", false);
            }
        });

        obj.find("#no-set-step").click(function () {
            if ($(this).is(':checked')) {
                obj.find("#step").val("");
                obj.find("#step").attr("disabled", true);
                that.ValueRule.step = "";
            } else {
                obj.find("#step").attr("disabled", false);
            }
        });

        obj.find("#is-integer").click(function () {
            if ($(this).is(':checked')) {
                that.ValueRule.is_integer = 1;
            } else {
                that.ValueRule.is_integer = 0;
            }
        });
    },
    saveValue: function () {
        if (!this.ValueRule) {
            this.ValueRule = {};
        }
        var obj = this.Element;
        var min = obj.find("#min").val();
        var max = obj.find("#max").val();
        var step = obj.find("#step").val();

        if (min != "" && max != "") {
            min = parseInt(min);
            max = parseInt(max);
            if (min >= max) {
                warningDialog("最小值不能大于等于最大值");
                return false;
            }
        }

        this.ValueRule.type = "number";
        this.ValueRule.min = min;
        this.ValueRule.max = max;
        this.ValueRule.step = step;

        if (obj.find("#is-integer").is(":checked")) {
            this.ValueRule.is_integer = 1;
        } else {
            this.ValueRule.is_integer = 0;
        }

        return this.ValueRule;
    },
    getValue: function () {
        return this.ValueRule;
    }
};


function PropertyEnum(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
}

PropertyEnum.prototype = {
    render: function () {
        this.Element.empty();
        var html = "";
        html += '<a type="button" class="btn btn-primary add-icon attribute-search-btn" id="proper-enum-add-btn"><div class="btn-icon"></div><div class="btn-text">添加</div></a>';
        html += '<table class="table table-bordered table-hover" id="property-rule-enum-table">';
        html += '<thead><th>枚举编码</th><th>枚举名称</th><th>图片地址</th><th>操作</th></tr></thead>';
        html += '<tbody></tbody>';
        html += '</table>';

        var obj = $(html);
        this.Element.append(obj);

        this.addBtnEvent(obj);
        var that = this;

        if (this.ValueRule) {
            //初始化枚举列表
            for (var i = 0; i < this.ValueRule.values.length; i++) {
                var value = this.ValueRule.values[i];
                var tr = '<tr>';
                tr += '<th>' + value.code + '</th>';
                tr += '<th>' + value.name + '</th>';

                var image_html = "";
                if (value.url) {
                    image_html = '<img style="width: 25px; height: 25px" src="' + value.url + '"/>';
                }

                tr += '<th>' + image_html + '</th>';
                tr += '<th><a class="btn btn-link" id="edit">编辑</a><a class="btn btn-link" id="delete">删除</a></th>';
                tr += '</tr>';

                var $tr = $(tr);

                $tr.find('#edit').click(function () {
                    //修改当前tr样式
                    var code = $(this).parent().prev().prev().prev().text();
                    var name = $(this).parent().prev().prev().text();
                    var name_th = '<input type="text" id="proper-enum-name" value="' + name + '">';
                    $(this).parent().prev().prev().empty().append($(name_th));

                    var image_url = $(this).parent().prev().find("img").attr("src");
                    if (!image_url) {
                        image_url = "";
                    }
                    var image_th = '<input type="text" id="proper-enum-url" value="' + image_url + '">';
                    $(this).parent().prev().empty().append($(image_th));

                    var operate = $('<a class="btn btn-link" id="save">保存</a><a class="btn btn-link" id="cancel">取消</a>');

                    $(operate[0]).click(function () {
                        var name = $(this).parent().prev().prev().find("#proper-enum-name").val();
                        if (name == "") {
                            warningDialog("名称不能为空");
                            return false;
                        }

                        var url = $(this).parent().prev().find("#proper-enum-url").val();
                        if (!url) {
                            url = "";
                        }
                        for (var i = 0; i < that.ValueRule.values.length; i++) {
                            var value = that.ValueRule.values[i];
                            if (value.code == code) {
                                value.name = name;
                                value.url = url;
                                that.render();
                                break;
                            }
                        }

                    });

                    $(operate[1]).click(function () {
                        that.render();
                    });

                    $(this).parent().empty().append(operate);

                });

                $tr.find('#delete').click(function () {
                    var code = $(this).parent().prev().prev().prev().text();
                    var remove_dx = 0;
                    for (var dx = 0; dx < that.ValueRule.values.length; dx++) {
                        var value = that.ValueRule.values[dx];
                        if (value.code == code) {
                            remove_dx = dx + 1;
                            break;
                        }
                    }
                    if (remove_dx) {
                        that.ValueRule.values.remove(remove_dx - 1);
                    }

                    $(this).parent().parent().remove();
                });

                obj.find("tbody").append($tr);
            }
        }
    },
    addBtnEvent: function (enum_obj) {
        var obj = enum_obj;
        var this_property = this;
        this.Element.find("#proper-enum-add-btn").click(function () {
            //判断是否存在有编辑的行，无则添加
            if ($(this).attr('data-status') == 'edit') {
                return false;
            }

            var that = $(this);
            var tr = '<tr>' +
                '<th><input type="text" id="proper-enum-code"></th>' +
                '<th><input type="text" id="proper-enum-name"></th>' +
                '<th><input type="text" id="proper-enum-url"></th>' +
                '<th> <a class="btn btn-link" id="save">保存</a><a class="btn btn-link" id="cancel">取消</a></th></tr>>';
            var $tr = $(tr);
            $tr.find("#save").click(function () {
                var code = $(this).parent().prev().prev().prev().find('input').val();
                var name = $(this).parent().prev().prev().find('input').val();
                var url = $(this).parent().prev().find('input').val();
                if (code == '' || name == '') {
                    warningDialog('编码或名称不能为空');
                    return false;
                }
                //添加到数据集合
                //判断是否有重复项
                if (this_property.ValueRule && this_property.ValueRule.values) {
                    for (var i = 0; i < this_property.ValueRule.values.length; i++) {
                        var value = this_property.ValueRule.values[i];
                        if (value.code == code) {
                            warningDialog('编码重复');
                            return false;
                        }
                    }
                } else {
                    this_property.ValueRule = {};
                    this_property.ValueRule.values = new Array();
                }
                this_property.ValueRule.values.push({code: code, name: name, url: url});
                this_property.render();

                that.attr('data-status', '');
            });

            $tr.find("#cancel").click(function () {
                $(this).parent().parent().remove();
                that.attr('data-status', '');
            });

            obj.find("tbody").append($tr);
            $(this).attr('data-status', 'edit');
        });
    },
    saveValue: function () {
        if (!this.ValueRule) {
            this.ValueRule = {};
        }
        this.ValueRule.type = "enum";
        if (!this.ValueRule.values || this.ValueRule.values.length == 0) {
            warningDialog("请输入值");
            return false;
        }

        return this.ValueRule;
    },
    getValue: function () {
        return this.ValueRule;
    }
};


function PropertyList(element, value_rule, property_id) {
    this.Element = element;
    this.ValueRule = value_rule;
    this.PropertyId = property_id;
}

PropertyList.prototype = {
    render: function () {
        this.Element.empty();
        var html = '';
        html += '<a type="button" class="btn btn-primary add-icon attribute-search-btn" id="proper-list-search-btn"><div class="btn-icon"></div><div class="btn-text">查询</div></a>';
        html += '<div class="form-horizontal"><div class="form-group">';
        html += '<div class="col-lg-3"><input type="text" class="form-control" id="table-name" placeholder="表名" value=""></div>';
        html += '<div class="col-lg-3"><input type="text" class="form-control" id="column-key" placeholder="字段名称" value=""></div>';
        html += '<div class="col-lg-3"><input type="text" class="form-control" id="column-name" placeholder="显示名称" value=""></div>';
        html += '<div class="col-lg-3"><input type="text" class="form-control" id="table-where" placeholder="过滤条件"></div>';
        html += '</div></div>';
        html += '<table class="table table-bordered table-hover" id="property-rule-list-table">';
        html += '<thead></thead>';
        html += '<tbody></tbody>';
        html += '</table>';

        var obj = $(html);
        this.Element.append(obj);

        var that = this;

        //绑定数据
        if (this.ValueRule) {
            var table_name = this.ValueRule.table_name;
            this.Element.find("#table-name").val(table_name);

            var table_where = this.ValueRule.where;
            this.Element.find("#table-where").val(table_where);

            var column_key = "";
            var column_name = "";
            for (var i = 0; i < this.ValueRule.columns.length; i++) {
                var column = this.ValueRule.columns[i];
                column_key += column["key"];
                column_name += column["name"];
                if (i != this.ValueRule.columns.length - 1) {
                    column_key += ",";
                    column_name += ",";
                }
            }

            this.Element.find("#column-key").val(column_key);
            this.Element.find("#column-name").val(column_name);

            if (!this.ValueRule.IsEdit) {
                var value_ids = new Array();
                var data1 = {};
                data1.property_id = this.PropertyId;
                requestJson('../product-bom/get-property-list-value-relation', data1, false, function (response) {
                    if (response.status == 0) {
                        if (response.data) {
                            for (var i = 0; i < response.data.length; i++) {
                                value_ids.push(response.data[i]);
                            }
                        }
                    }
                });

                this.ValueRule.value_ids = value_ids;
            }
            if (this.ValueRule.value_ids) {
                var data = {};
                data.table_name = table_name;
                data.columns = column_key;
                data.where = table_where;

                requestJson('../product-bom/get-datas-by-talbe-name-handler', data, true, function (response) {
                    if (response.status == 0) {
                        var thead = that.Element.find("thead");
                        var tbody = that.Element.find("tbody");
                        var html_thead = '<tr><th><input type="checkbox" class="check-box" id="checkAll"></th>';

                        var names = that.Element.find("#column-name").val().split(",");
                        var keys = that.Element.find("#column-key").val().split(",");
                        for (var i = 0; i < names.length; i++) {
                            var name = names[i];
                            if (name.toLowerCase() != "id") {
                                html_thead += "<th>" + name + "</th>";
                            }
                        }

                        html_thead += "</tr>";

                        thead.append($(html_thead));

                        for (var i = 0; i < response.data.length; i++) {
                            var item = response.data[i];
                            var html_tbody = '';
                            html_tbody += '<tr>';
                            var check_str = "";
                            for (var j = 0; j < that.ValueRule.value_ids.length; j++) {
                                var value_id = that.ValueRule.value_ids[j];
                                if (value_id == item["id"]) {
                                    check_str = 'checked="checked"';
                                    break;
                                }
                            }

                            html_tbody += '<td><input type="checkbox" name="list-checkbox" class="check-box" ' + check_str + ' value="' + item["id"] + '"></td>>';
                            for (var j = 0; j < keys.length; j++) {
                                if (keys[j].toLowerCase() != "id") {
                                    html_tbody += '<td>' + item[keys[j]] + '</td>';
                                }
                            }
                            html_tbody += '</tr>';
                            tbody.append($(html_tbody));
                        }

                        that.Element.find("#checkAll").click(function () {
                            if ($(this).is(':checked')) {
                                that.Element.find('input[type=checkbox]').prop("checked", true);
                            } else {
                                that.Element.find('input[type=checkbox]').prop("checked", false);
                            }
                        });

                    } else {
                        errorDialog(response.message);
                    }
                });
            }
        }

        this.Element.find("#proper-list-search-btn").click(function () {
            var table_name = that.Element.find("#table-name").val().trim();
            var column_key = that.Element.find("#column-key").val().trim();
            var column_name = that.Element.find("#column-name").val().trim();
            var where = that.Element.find("#table-where").val().trim().toLowerCase();
            if (table_name == "" || table_name == null) {
                warningDialog("表名不能为空");
                return false;
            }

            var keys = column_key.split(",");
            var names = column_name.split(",");
            if (keys.length != names.length) {
                warningDialog("名称和显示名称数量不相同");
                return false;
            }

            if (where.length > 0) {
                if (where.indexOf('=') >= 0 && where.indexOf('and') < 0) {
                    var field = where.split("=")[0].trim();
                    var first = field.substr(0, 1);
                    var last = field.substr(-1);
                    if (first != "`" || last != "`") {
                        warningDialog('字段设置使用MySQL 关键字符串 “`”');
                        return false;
                    }
                } else if (where.indexOf('and') >= 0 && where.indexOf('=') >= 0) {
                    var wheres = where.split("and");
                    for (var i = 0; i < wheres.length; i++) {
                        var field = wheres[i].split("=")[0].trim();
                        var first = field.substr(0, 1);
                        var last = field.substr(-1);
                        if (first != "`" || last != "`") {
                            warningDialog('字段设置使用MySQL 关键字符串 “`”');
                            return false;
                        }
                    }
                } else {
                    warningDialog('语法错误');
                    return false;
                }
            }

            var params = {};
            params.table_name = table_name;
            params.columns = "";
            for (var i = 0; i < keys.length; i++) {
                params.columns += keys[i];
                if (i != keys.length - 1) {
                    params.columns += ",";
                }
            }

            params.where = where;
            requestJson('../product-bom/get-datas-by-talbe-name-handler', params, true, function (response) {
                if (response.status == 0) {
                    var thead = that.Element.find("thead");
                    var tbody = that.Element.find("tbody");
                    thead.empty();
                    tbody.empty();
                    var html_thead = '<tr><th><input type="checkbox" class="check-box" id="checkAll"></th>';

                    var names = that.Element.find("#column-name").val().split(",");
                    var keys = that.Element.find("#column-key").val().split(",");
                    for (var i = 0; i < names.length; i++) {
                        var name = names[i];
                        if (name.toLowerCase() != "id") {
                            html_thead += "<th>" + name + "</th>";
                        }
                    }

                    html_thead += "</tr>";


                    thead.append($(html_thead));

                    for (var i = 0; i < response.data.length; i++) {
                        var item = response.data[i];
                        var html_tbody = '';
                        html_tbody += '<tr>';
                        html_tbody += '<td><input type="checkbox" name="list-checkbox" class="check-box" value="' + item["id"] + '"></td>>';
                        for (var j = 0; j < keys.length; j++) {
                            if (keys[j].toLowerCase() != "id") {
                                html_tbody += '<td>' + item[keys[j]] + '</td>';
                            }
                        }
                        html_tbody += '</tr>';
                        tbody.append($(html_tbody));
                    }

                    addNewData(that);

                    that.Element.find("#checkAll").click(function () {
                        if ($(this).is(':checked')) {
                            that.Element.find('input[type=checkbox]').prop("checked", true);
                        } else {
                            that.Element.find('input[type=checkbox]').prop("checked", false);
                        }
                    });


                } else {
                    errorDialog(response.message);
                }
            });
        });

        if (this.ValueRule != undefined) addNewData(this);

    },
    saveValue: function () {
        if (!this.ValueRule) {
            this.ValueRule = {};
        }
        this.ValueRule.type = "list";

        var table_name = this.Element.find("#table-name").val();
        var column_key = this.Element.find("#column-key").val();
        var column_name = this.Element.find("#column-name").val();
        var where = this.Element.find("#table-where").val();
        where = where.replace(/'([^"]*)'/g, "\"$1\"");

        var keys = column_key.split(",");
        var names = column_name.split(",");
        if (keys.length != names.length) {
            warningDialog("名称和显示名称数量不相同");
            return false;
        }

        this.ValueRule.table_name = table_name;
        this.ValueRule.where = where;

        if (!this.ValueRule.columns) {
            this.ValueRule.columns = new Array();
        }
        this.ValueRule.columns.length = 0;
        for (var i = 0; i < keys.length; i++) {
            this.ValueRule.columns.push({"key": keys[i], "name": names[i]});
        }


        if (this.ValueRule.value_ids) {
            this.ValueRule.value_ids.length = 0;
        } else {
            this.ValueRule.value_ids = new Array();
        }

        var checks = this.Element.find("#property-rule-list-table").find("input[name='list-checkbox']:checkbox:checked");

        for (var i = 0; i < checks.length; i++) {
            var check = checks[i];
            this.ValueRule.value_ids.push($(check).val());
        }

        this.ValueRule.IsEdit = true;
        return this.ValueRule;
    },
    getValue: function () {
        return this.ValueRule;
    }
};


function PropertyTable(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
}

PropertyTable.prototype = {
    render: function () {
        this.Element.empty();
        var html = '';
        html += '<div class="form-horizontal" id="property_table_form">';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="table-name">表名：</label>';
        html += '<div class="col-lg-10"><input type="text" id="table-name" name="table-name" class="form-control" value=""></div>';
        html += '</div>';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="column-key">字段名称：</label>';
        html += '<div class="col-lg-10"><input type="text" id="column-key" name="column-key" class="form-control" value=""></div>';
        html += '</div>';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="column-name">字段显示名称：</label>';
        html += '<div class="col-lg-10"><input type="text" id="column-name" name="column-name" class="form-control" value=""></div>';
        html += '</div>';

        html += '<div class="form-group"><label class="control-label col-lg-2" for="table-where">过滤条件：</label>';
        html += '<div class="col-lg-10"><input type="text" id="table-where" name="table-where" class="form-control" value=""></div>';
        html += '</div>';

        html += '</div>';

        var obj = $(html);
        this.Element.append(obj);

        var that = this;
        //初始化数据
        if (this.ValueRule) {
            obj.find("#table-name").val(this.ValueRule.table_name);
            var column_key = "";
            var column_name = "";
            for (var i = 0; i < this.ValueRule.columns.length; i++) {
                column_key += this.ValueRule.columns[i].key;
                column_name += this.ValueRule.columns[i].name;
                if (i < this.ValueRule.columns.length - 1) {
                    column_key += ",";
                    column_name += ",";
                }
            }
            obj.find("#column-key").val(column_key);
            obj.find("#column-name").val(column_name);
            obj.find("#table-where").val(this.ValueRule.where);
        }
    },
    saveValue: function () {
        if (!this.ValueRule) {
            this.ValueRule = {};
        }
        this.ValueRule.type = "table";

        var obj = this.Element;
        var table_name = obj.find("#table-name").val();
        var column_key = obj.find("#column-key").val();
        var column_name = obj.find("#column-name").val();
        var where = obj.find("#table-where").val();
        where = where.replace(/'([^"]*)'/g, "\"$1\"");

        this.ValueRule.table_name = table_name;
        var keys = column_key.split(",");
        var names = column_name.split(",");
        if (keys.length != names.length) {
            warningDialog("名称和显示名称数量不相同");
            return false;
        }
        if (!this.ValueRule.columns) {
            this.ValueRule.columns = new Array();
        }
        this.ValueRule.columns.length = 0;
        for (var i = 0; i < keys.length; i++) {
            this.ValueRule.columns.push({"key": keys[i], "name": names[i]});
        }

        this.ValueRule.where = where;
        return this.ValueRule;
    },
    getValue: function () {
        return this.ValueRule;
    }
};


function PropertyFile(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
}

PropertyFile.prototype = {
    render: function () {
        this.Element.empty();

        var html = "";
        html += '<div class="form-horizontal" id="property_number_form">';

        html += '<div class="form-group"><label class="control-label col-lg-2">说明：</label>';
        html += '<div class="col-lg-8" style="padding-top: 7px;">无配置，前端展示文件上传控件</div>';
        html += '</div>';

        html += '</div>';
        var obj = $(html);

        this.Element.append(obj);
    },
    saveValue: function () {
        if (!this.ValueRule) {
            this.ValueRule = {};
        }
        this.ValueRule.type = "file";
        return this.ValueRule;
    },
    getValue: function () {
        return this.ValueRule;
    }
};


function PropertyAuto(element, value_rule) {
    this.Element = element;
    this.ValueRule = value_rule;
}

PropertyAuto.prototype = {
    render: function () {
        this.Element.empty();

        var html = "";
        html += '<div class="form-horizontal" id="property_number_form">';

        html += '<div class="form-group"><label class="control-label col-lg-2">说明：</label>';
        html += '<div class="col-lg-8" style="padding-top: 7px;">使用公式进行计算</div>';
        html += '</div>';

        html += '</div>';
        var obj = $(html);

        this.Element.append(obj);
    },
    saveValue: function () {
        if (!this.ValueRule) {
            this.ValueRule = {};
        }

        this.ValueRule.type = "auto";
        return this.ValueRule;
    },
    getValue: function () {
        return this.ValueRule;
    }
};

function addNewData(obj) {
    $("#is_new_add_a").remove();
    var category_id = '';
    var action = '';
    var table_name = obj.Element.find("#table-name").val();
    var where = obj.Element.find("#table-where").val();

    if (where.length > 0) {

        var where_str = where.toLowerCase();
        if (where_str.indexOf('and') >= 0) {
            var wheres = where_str.split('and');
            for (var k = 0; k < wheres.length; k++) {
                if (wheres[k].indexOf('data_category_id') >= 0) {
                    category_id = wheres[k].split('=')[1].trim();
                    break;
                }
            }
        } else {
            var wheres = where_str.split('=');
            for (var m = 0; m < wheres.length; m++) {
                if (wheres[m].indexOf('data_category_id') >= 0) {
                    category_id = wheres[m + 1].trim();
                    break;
                }
            }

        }

    }

    var actions = table_name.split('_');
    actions.splice(0, 2);

    for (var k = 0; k < actions.length; k++) {
        action += '-' + actions[k];
    }
    action = action.substr(1);
    if (action == "data" || action == "post-printing-data") {
        action = 'base-' + action;
    }
    var html_a = '';
    html_a += '<a id="is_new_add_a" class="btn btn-primary putaway-icon" style="float: left"';
    if (category_id != '') {
        html_a += 'onclick=onNewModalEdit(\"' + action + '\",' + category_id + ')>';
    } else {
        html_a += 'onclick=onNewModalEdit(\"' + action + '\")>';
    }
    html_a += '<div class="btn-icon"></div><div class="btn-text">添加更多</div></a>';
    $(html_a).prependTo("#propertyRuleModalLabelFooter");
    return true;
}